package com.daoxiang.controller;

import com.daoxiang.pojo.ReceivingAddress;
import com.daoxiang.pojo.ReceivingAddressCustom;
import com.daoxiang.pojo.User;
import com.daoxiang.service.ReceivingAddressCustomService;
import com.daoxiang.service.ReceivingAddressService;
import com.daoxiang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class ReceivingAddressController {

    @Autowired
    private ReceivingAddressService receivingAddressService;

    @Autowired
    private ReceivingAddressCustomService receivingAddressCustomService;

    @Autowired
    private UserService userService;

    @PostMapping("/ajaxAddReceivingAddress")
    public ResponseEntity<String> ajaxAddReceivingAddress(ReceivingAddress receivingAddress, Integer rauid) {
        receivingAddress.setUid(rauid);
        int i = this.receivingAddressService.insertReceivingAddress(receivingAddress);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

    @GetMapping("/queryAllReceivingAddress")
    public ResponseEntity<List<ReceivingAddressCustom>> queryAllReceivingAddress(ReceivingAddressCustom receivingAddressCustom) throws InterruptedException {
        List<ReceivingAddressCustom> receivingAddressCustomList = this.receivingAddressCustomService.queryReceivingAddressParams(receivingAddressCustom);
        TimeUnit.MILLISECONDS.sleep(100);
        return new ResponseEntity<List<ReceivingAddressCustom>>(receivingAddressCustomList, HttpStatus.OK);
    }

    @PutMapping("/ajaxUpdateUser")
    public ResponseEntity<String> ajaxUpdateUser(User user) {
        int i = this.userService.updateUserUidAndDefaultAddress(user);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

    @PutMapping("/ajaxEditReceivingAddress")
    public ResponseEntity<String> ajaxEditReceivingAddress(ReceivingAddress receivingAddress) {
        int i = this.receivingAddressService.updateReceivingAddress(receivingAddress);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

}
